<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : customer  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrcustomer extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
        //$this->load->view('index_aca',$data);
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformcustomer('0', $xAwal);
    }

    function createformcustomer($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        
        $xAddJs =                 
                link_tag('resource/css/admin/upload/css/upload.css') . "\n" .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/js/common/fileupload/jquery.knob.js"></script>' . "\n" .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/js/common/fileupload/jquery.ui.widget.js"></script>' . "\n" .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/js/common/fileupload/jquery.iframe-transport.js"></script>' . "\n" .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/js/common/fileupload/jquery.fileupload.js"></script>' . "\n" .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/js/common/fileupload/myupload.js"></script>' . "\n" .                
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxcustomer.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormcustomer($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormcustomer($xidx) {
        $this->load->helper('form');
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>customer</h3><div class="garis"></div>' . form_open_multipart('ctrcustomer/inserttable', array('id' => 'form', 'name' => 'form'));
        $this->load->helper('common');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
        $this->load->model('modelcountry');



        $xBufResult .= '<div id="tabs">
                                 <ul>
                                   <li><a href="#dataumum"><span>Name And Address</span></a></li>
                                   <li><a href="#dataumum2"><span>Postal Adress</span></a></li>
                                   <li><a href="#dataumum3"><span>User And Password</span></a></li>
                                   <li><a href="#uploadfile"><span>Upload File Excel</span></a></li>
                                  </ul>';
        $xBufResult .= '<div id="dataumum">';
        $xBufResult .= setForm('edtitle', 'Title', form_input(getArrayObj('edtitle', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edfirstname', 'First Name', form_input(getArrayObj('edfirstname', '', '100')));
        $xBufResult .= setForm('edmidlename', 'Midle Name', form_input(getArrayObj('edmidlename', '', '100')));
        $xBufResult .= setForm('edlastname', 'Last Name', form_input(getArrayObj('edlastname', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edorganization', 'Organization', form_input(getArrayObj('edorganization', '', '400'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edposition', 'Position', form_input(getArrayObj('edposition', '', '300'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edaddress', 'Address', form_textarea(getArrayObj('edaddress', '', '400'))) . '<div class="spacer"></div>';
        $xBufResult .= '</div><div id="dataumum2">';
        $xBufResult .= setForm('ediddepartemen', 'Country', form_dropdown('edidcountry', $this->modelcountry->getArrayListcountry(), '', ' id="edidcountry" onChange="dosetstate(\'0\');" class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edidstate', 'State', form_dropdown('edidstate', array(NULL), '', ' id="edidstate" onChange="dosetcity(\'0\');" class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edidcity', 'City', form_dropdown('edidcity', array(NULL), '', ' id="edidcity"  class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
//         $xBufResult .= setForm('edidcountry', 'idcountry', form_input(getArrayObj('edidcountry', '', '100'))) . '<div class="spacer"></div>'; 
//         $xBufResult .= setForm('edidstate', 'idstate', form_input(getArrayObj('edidstate', '', '100'))) . '<div class="spacer"></div>';
//         $xBufResult .= setForm('edidcity', 'idcity', form_input(getArrayObj('edidcity', '', '100'))) . '<div class="spacer"></div>';


        $xBufResult .= setForm('edzipcode', 'Zip Code', form_input(getArrayObj('edzipcode', '', '200')));
        $xBufResult .= setForm('edtelepon', 'Telepon', form_input(getArrayObj('edtelepon', '', '200'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edmobile', 'Mobile', form_input(getArrayObj('edmobile', '', '200')));
        $xBufResult .= setForm('edfax', 'Fax', form_input(getArrayObj('edfax', '', '200'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edemail', 'Email', form_input(getArrayObj('edemail', '', '300'))) . '<div class="spacer"></div>';
        $xBufResult .= '</div><div id="dataumum3">';
        $xBufResult .= setForm('edusername', 'username', form_input(getArrayObj('edusername', '', '150'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edpassword', 'password', form_input(getArrayObj('edpassword', '', '150'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('eddateentry', 'dateentry', form_input(getArrayObj('eddateentry', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edstatus', 'status', form_input(getArrayObj('edstatus', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edidcity', 'Status ', form_dropdown('edstatus', getstatusanggota(), '', ' id="edstatus"  class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
        
        $xBufResult .= '</div><div id="uploadfile">';
        $xBufResult .= setForm('linkfile', 'Uplload File Excel', '<div id="uploadareajaminan"><br />' . form_input(getArrayObj('eduploadfileexcel', '', '100'), '', 'alt="Upload File Excel"') . '</div>') . '<div class="spacer"></div>';
        $xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'Import From Excel', 'onclick="doimportexcel();"') . '<div class="spacer"></div>';
        $xBufResult .= '</div>';
        $xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'simpan', 'onclick="dosimpancustomer();"') . form_button('btNew', 'new', 'onclick="doClearcustomer();"') . '<div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '') . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getlistcustomer($xAwal, $xSearch) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('idx', '', 'width=5%') .
                tbaddcell('Name', '', 'width=30%') .
                tbaddcell('organization', '', 'width=10%') .
                tbaddcell('position', '', 'width=10%') .
                tbaddcell('address', '', 'width=40%') .
                tbaddcell('Edit/Hapus', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelcustomer');
        $xQuery = $this->modelcustomer->getListcustomer($xAwal, $xLimit, $xSearch);
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doeditcustomer(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuscustomer(\'' . $row->idx . '\',\'' . substr($row->title, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($row->idx) .
                    tbaddcell($row->title . ' ' . $row->firstname . ' ' . $row->midlename . ' ' . $row->lastname) .
                    tbaddcell($row->organization) .
                    tbaddcell($row->position) .
                    tbaddcell($row->address) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchcustomer(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editreccustomer() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelcustomer');
        $row = $this->modelcustomer->getDetailcustomer($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['title'] = $row->title;
        $this->json_data['firstname'] = $row->firstname;
        $this->json_data['midlename'] = $row->midlename;
        $this->json_data['lastname'] = $row->lastname;
        $this->json_data['organization'] = $row->organization;
        $this->json_data['position'] = $row->position;
        $this->json_data['address'] = $row->address;
        $this->json_data['idcity'] = $row->idcity;
        $this->json_data['idstate'] = $row->idstate;
        $this->json_data['idcountry'] = $row->idcountry;
        $this->json_data['zipcode'] = $row->zipcode;
        $this->json_data['telepon'] = $row->telepon;
        $this->json_data['mobile'] = $row->mobile;
        $this->json_data['fax'] = $row->fax;
        $this->json_data['email'] = $row->email;
        $this->json_data['username'] = $row->username;
        $this->json_data['password'] = $row->password;
        $this->json_data['dateentry'] = $row->dateentry;
        $this->json_data['idpegawai'] = $row->idpegawai;
        $this->json_data['status'] = $row->status;
        echo json_encode($this->json_data);
    }

    function deletetablecustomer() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelcustomer');
        $this->modelcustomer->setDeletecustomer($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchcustomer() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatacustomer'] = $this->getlistcustomer($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }

    function simpancustomer() {
        $this->load->helper('json');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $xtitle = $_POST['edtitle'];
        $xfirstname = $_POST['edfirstname'];
        $xmidlename = $_POST['edmidlename'];
        $xlastname = $_POST['edlastname'];
        $xorganization = $_POST['edorganization'];
        $xposition = $_POST['edposition'];
        $xaddress = $_POST['edaddress'];
        $xidcity = $_POST['edidcity'];
        $xidstate = $_POST['edidstate'];
        $xidcountry = $_POST['edidcountry'];
        $xzipcode = $_POST['edzipcode'];
        $xtelepon = $_POST['edtelepon'];
        $xmobile = $_POST['edmobile'];
        $xfax = $_POST['edfax'];
        $xemail = $_POST['edemail'];
        $xusername = $_POST['edusername'];
        $xpassword = $_POST['edpassword'];
        $xdateentry = $_POST['eddateentry'];
        $xidpegawai = $_POST['edidpegawai'];
        $xstatus = $_POST['edstatus'];
        $this->load->model('modelcustomer');
        $idpegawai = $this->session->userdata('idpegawai');
        if (!empty($idpegawai)) {
            if ($xidx != '0') {
                $xStr = $this->modelcustomer->setUpdatecustomer($xidx, $xtitle, $xfirstname, $xmidlename, $xlastname, $xorganization, $xposition, $xaddress, $xidcity, $xidstate, $xidcountry, $xzipcode, $xtelepon, $xmobile, $xfax, $xemail, $xusername, $xpassword, $xdateentry, $idpegawai, $xstatus);
            } else {
                $xStr = $this->modelcustomer->setInsertcustomer($xidx, $xtitle, $xfirstname, $xmidlename, $xlastname, $xorganization, $xposition, $xaddress, $xidcity, $xidstate, $xidcountry, $xzipcode, $xtelepon, $xmobile, $xfax, $xemail, $xusername, $xpassword, $xdateentry, $idpegawai, $xstatus);
            }
        }
        echo json_encode(null);
    }

    function dosetstate() {
        $this->load->model('modelstate');
        $xidcountry = $_POST['xidcountry'];
        $this->load->helper('json');
        $this->json_data['optionstate'] = $this->modelstate->getoptionstatebycountry($xidcountry);
        echo json_encode($this->json_data);
    }

    function dosetcity() {
        $this->load->model('modelcity');
        $xidstate = $_POST['xidstate'];
        $this->load->helper('json');
        $this->json_data['optioncity'] = $this->modelcity->getoptionstatebyidstate($xidstate);
        echo json_encode($this->json_data);
    }
 
    function doimportexcel(){
     $xnmfile = $_POST['fileexcel'];     
     $this->load->library('libexcel');
     $reader =PHPExcel_IOFactory::load("./././resource/uploaded/file/".$xnmfile);   
     $xtrue = TRUE;
     $irowexcel = 2;
     while ($xtrue) {
     
          $xtitle = $reader->setActiveSheetIndex(0)->getCell("B" . $irowexcel)->getValue();
          $xfirstname = $reader->setActiveSheetIndex(0)->getCell("C" . $irowexcel)->getValue();
          if(empty($xfirstname)){
             $xtrue = FALSE;   
          }          
                  $xmidlename = $reader->setActiveSheetIndex(0)->getCell("D" . $irowexcel)->getValue();
                  $xlastname = $reader->setActiveSheetIndex(0)->getCell("E" . $irowexcel)->getValue();
                  $xorganization =  $reader->setActiveSheetIndex(0)->getCell("F" . $irowexcel)->getValue();
                  $xposition =  $reader->setActiveSheetIndex(0)->getCell("G" . $irowexcel)->getValue();
                  $xaddress =  $reader->setActiveSheetIndex(0)->getCell("H" . $irowexcel)->getValue();
                  $xzipcode =  $reader->setActiveSheetIndex(0)->getCell("L" . $irowexcel)->getValue();
                  $xtelepon =  $reader->setActiveSheetIndex(0)->getCell("M" . $irowexcel)->getValue();
                  $xmobile =  $reader->setActiveSheetIndex(0)->getCell("N" . $irowexcel)->getValue();
                  $xfax =  $reader->setActiveSheetIndex(0)->getCell("O" . $irowexcel)->getValue();
                  $xemail =  $reader->setActiveSheetIndex(0)->getCell("P" . $irowexcel)->getValue();
                  
//                           , , $xidcity, $xidstate, $xidcountry, $xzipcode,  $xtelepon, $xmobile, $xfax, $xemail, $xusername, $xpassword, $xdateentry, $idpegawai, $xstatus
             if($xtrue){
              $this->load->model('modelcustomer');   
              $this->modelcustomer->setInsertcustomer('0', $xtitle, $xfirstname, $xmidlename, $xlastname, $xorganization, $xposition, $xaddress, '0', '0', '0', 
                      $xzipcode, $xtelepon, $xmobile, $xfax, $xemail, '', '', '', '', '1');
             }     
     $irowexcel++;             
     }
     $this->load->helper('json');
     echo json_encode(null);
    }
}

?>